﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using League.Entity;
using League.DataAccess;
using JJY.CommonHelper;
using System.Data;

public partial class Page_Versus : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LeagueBind();
            TeamBind(Guid.Parse(ddlLeague.SelectedValue));
        }
    }

    private void LeagueBind()
    {
        List<LeagueInfo> leagueInfoSet = LeagueInfoDataAccess.GetAllLeagueInfo();
        foreach (LeagueInfo leagueInfo in leagueInfoSet)
        {
            ddlLeague.Items.Add(new ListItem(leagueInfo.SeasonYear.ToString() + " - " +
                leagueInfo.LeagueName, leagueInfo.LeagueID.ToString()));
        }
    }

    private void TeamBind(Guid leagueId)
    {
        List<LeagueTeam> LeagueTeamSet = LeagueTeamDataAccess.GetAllTeamByLeagueID(leagueId);
        ddlHome.Items.Clear();
        ddlVisiting.Items.Clear();
        foreach (LeagueTeam leagueTeam in LeagueTeamSet)
        {
            ddlHome.Items.Add(new ListItem(leagueTeam.TeamName, leagueTeam.TeamID.ToString()));
            ddlVisiting.Items.Add(new ListItem(leagueTeam.TeamName, leagueTeam.TeamID.ToString()));
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {

    }

    protected void ddlLeague_SelectedIndexChanged(object sender, EventArgs e)
    {
        TeamBind(Guid.Parse(ddlLeague.SelectedValue));
    }

    private DataTable ColumnToRow(DataTable dataTable, string header, string[] rowNameSet)
    {
        DataTable result = new DataTable();

        result.Columns.Add("Title");
        for (int i = dataTable.Rows.Count; i > 0; i--)
        {
            result.Columns.Add(i.ToString());
        }

        DataRow[] dataRowSet = new DataRow[rowNameSet.Length];

        for (int i = 0; i < dataRowSet.Length; i ++)
        {
            dataRowSet[i] = result.NewRow();
            dataRowSet[i][0] = rowNameSet[i];
        }

        for (int i = 0; i < dataTable.Rows.Count; i++)
        {
            for (int j = 0; j < rowNameSet.Length; j++)
            {
                dataRowSet[j][i + 1] = dataTable.Rows[i][rowNameSet[j]];
            }
        }

        foreach (DataRow dr in dataRowSet)
        {
            result.Rows.Add(dr);
        }

        return result;
    }

    protected void LeagueTeam_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataTable dtHome = LeagueTeamDataDataAccess.TeamHistory(ddlHome.SelectedValue);
        DataTable dtVisiting = LeagueTeamDataDataAccess.TeamHistory(ddlVisiting.SelectedValue);


        DataView dvHome = dtHome.DefaultView;
        dvHome.Sort = "MatchDateTime DESC";

        DataView dvVisiting = dtVisiting.DefaultView;
        dvVisiting.Sort = "MatchDateTime DESC";


        string header = "MatchDateTime";
        string[] columnSet = {"TeamName","HomeTeam", "VisitingTeam","MatchResult","IsAtHome","Goal","Shot","ShotOnTarget",
                                 "BlockedShot","Corner","FreeKick","OutOfBounds","Foul","Offside","BallControl",
                                 "PassSuccess","PassMiddleSuccess","TacklingSuccess","HeadSuccess","YellowCard",
                                 "RedCard"};

        DataTable dtNewHome = ColumnToRow(dtHome, header, columnSet);
        DataTable dtNewVisiting = ColumnToRow(dtVisiting, header, columnSet);

        panel.InnerHtml = TableCreater.Create(dtNewHome, "tableHome") +
            "<br />" + TableCreater.Create(dtNewVisiting, "tableVisiting");
    }

    private void test()
    {
 
    }
}